package edu.lsnu.equipment_management_system.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author mzywucai
 * @Description
 * @date 2018/12/7
 */
public class MyStringUtil {
    /**
     * @Description 判断一个字符串是否都为数字
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return
     * @params
     */
    public static boolean isDigit(String strNum) {
        return strNum.matches("[0-9]{1,}");
    }

    /**
     * @Description 截取数字
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   java.lang.String
     * @params  [content]
     */
    public static String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    /**
     * @Description 截取非数字
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   java.lang.String
     * @params  [content]
     */
    public static String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    /**
     * @Description 判断一个字符串是否含有数字
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   boolean
     * @params  [content]
     */
    public static boolean HasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * @Description 校验是否全是数字：通过java默认的Character的isDigit方法
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   boolean
     * @params  [str]
     */
    public static boolean isNumericByJavaDefault(String str) {
        for (int i = str.length(); --i >= 0;) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * @Description 校验是否全是数字：通过正则表达式
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   boolean
     * @params  [strNum]
     */
    public boolean isNumericByPattern(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * @Description 校验是否全是数字：通过ASCII
     * @author mzywucai
     * @time 2018-12-07
     * @see
     * @return   boolean
     * @params  [str]
     */
    public static boolean isNumericByAscii(String str) {
        for (int i = str.length(); --i >= 0;) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }
}
